package com.ciwong.mobilelib.widget;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.NinePatch;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.mobilelib.R;

import java.io.InputStream;

/**
 * Created by keepon on 2018/4/2.
 */

public class NewCWProgressBar extends ProgressDialog {
	private Context      mContext;
	private LinearLayout contentView;
	private ImageView    imageView;
	private TextView     textView;

	public NewCWProgressBar(Context context) {
		super(context,R.style.my_progress_dialog);
		this.mContext = context;
		this.setCancelable(true);
		this.contentView = new LinearLayout(context);
		this.contentView.setOrientation(LinearLayout.HORIZONTAL);
		this.contentView.setGravity(16);
		LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(-1, -1);
		this.contentView.setLayoutParams(llp);
		this.addImageView();
		this.addTextView();
	}

	public void setMessage(CharSequence message) {
		this.textView.setText(message);
		this.textView.setVisibility(View.VISIBLE);
	}

	public void setMessage(int resId) {
		this.setMessage(this.mContext.getString(resId));
	}

	public void show() {
		try {
			super.show();
			Animation rotaAnimation = new RotateAnimation(0.0F, 357.0F, 1, 0.5F, 1, 0.5F);
			this.imageView.clearAnimation();
			this.imageView.setAnimation(rotaAnimation);
			rotaAnimation.setRepeatCount(-1);
			rotaAnimation.setDuration(2000L);
			rotaAnimation.setFillAfter(false);
			rotaAnimation.setInterpolator(new LinearInterpolator());
			rotaAnimation.startNow();
			this.setContentView(this.contentView);
		}catch (Exception e){

		}

	}

	private void addTextView() {
		this.textView = new TextView(this.mContext);
		this.textView.setGravity(17);
		this.textView.setTextColor(-1);
		this.textView.setTextSize(2, 14.0F);
		LinearLayout.LayoutParams llp_tv = new LinearLayout.LayoutParams(-1, -1);
		llp_tv.gravity = 1;
		this.textView.setLayoutParams(llp_tv);
		this.textView.setVisibility(View.GONE);
		this.contentView.addView(this.textView);
	}

	private void addImageView() {
		LinearLayout.LayoutParams llp_bar = new LinearLayout.LayoutParams(70, 70);
		llp_bar.gravity = 17;
//		Drawable bmp = getDrawableFromJar("/images_progressbar/loading.png", CWProgressBar.class);
		this.imageView = new ImageView(this.mContext);
		this.imageView.setBackgroundResource(R.mipmap.loading);
		this.imageView.setLayoutParams(llp_bar);
		this.contentView.addView(this.imageView);
	}

	public static Drawable getDrawableFromJar(String path, Class<?> clazz) {
		return getDrawableFromJar(path, clazz, new Rect(20, 20, 20, 20));
	}

	public static Drawable getDrawableFromJar(String path, Class<?> clazz, Rect padding) {
		InputStream inputstream = clazz.getResourceAsStream(path);
		if(inputstream != null) {
			Bitmap localBitmap = BitmapFactory.decodeStream(inputstream);
			return getNinePathDrawable(localBitmap, padding);
		} else {
			return null;
		}
	}

	public static Drawable getNinePathDrawable(Bitmap localBitmap, Rect padding) {
		if(localBitmap != null) {
			byte[] chunk = localBitmap.getNinePatchChunk();
			boolean result = NinePatch.isNinePatchChunk(chunk);
			if(result) {
				NinePatchDrawable patchy = new NinePatchDrawable(localBitmap, chunk, padding, (String)null);
				patchy.setCallback((Drawable.Callback)null);
				return patchy;
			}
		}

		return new BitmapDrawable(localBitmap);
	}
}
